package com.i2bt.app.friend;

import com.i2bt.common.AndroidRestContract;
import com.i2bt.common.Contract;

import com.i2bt.R;
import com.i2bt.adapter.FriendDetailSharedListAdapter;
import com.i2bt.adapter.SharedFriendListAdapter;
import com.i2bt.adapter.SharedMyListAdapter;
import com.i2bt.app.BaseActivity;
import com.i2bt.app.friend.model.FriendDetailJsonHandle;
import com.i2bt.app.share.ShareChangeShareActivity;
import com.i2bt.app.share.model.FriendShareJsonHandle;
import com.i2bt.app.share.model.GetMyShareJsonHandle;
import com.i2bt.app.share.model.GetShareJsonHandle;
import com.i2bt.app.share.model.MyShareJsonHandle;
import com.i2bt.bean.FriendBean;
import com.i2bt.bean.FriendInfoBean;
import com.i2bt.bean.GetSharedBean;
import com.i2bt.bean.ShareBean;
import com.i2bt.util.AlertDialogHelp;
import com.i2bt.util.VisibilityUtil;
import com.i2bt.view.XListView;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.List;

/**
 * 
 * Title : 好友详情活动类
 *
 * Description : 显示和操作分享给好友的资源以及好友分享给自己的资源
 *
 * CreatedTime : 2014-8-21
 *
 * @author wuliao & kakaxi & yi
 *
 * @version 1.0
 *
 */

public class FriendDetailActivity extends BaseActivity {
	
	private TextView friendNameText; // 好友昵称文本视图 
	
	private Button friendConcernBtn; // 取消关注按钮
	
	private FriendInfoBean friendInfoBean;  //传递过来的好友信息bean
	
	private XListView sharedMyList; // 自己分享给好友的列表
	
	private FriendDetailSharedListAdapter sharedMyListAdapter; // 自己分享给好友的列表适配器
	
	private XListView shareFriendList; // 好友分享给自己的列表
	
	private SharedFriendListAdapter sharedFriendListAdapter; // 好友分享给自己的列表适配器
	
	private RadioButton mySharedRadioButton; // 我的分享按钮
	
	private RadioButton friendSharedRadioButton; // 好友分享按钮

	private FriendDetailJsonHandle friendDetailJsonHandle; // 好友详情json处理器
	
	private List<GetSharedBean> myShareData; // 自己分享的数据
	
	private List<GetSharedBean> friendSharedData; // 好友分享的数据
	
	private boolean isDeleted; // 是否有取消好友
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_friend_detail);
		
		initView();
		logic();
	}

	@SuppressLint("NewApi")
	@Override	
	protected void initView() {
		friendNameText = (TextView) findViewById(R.id.friend_detail_name);
		
		friendConcernBtn = (Button) findViewById(R.id.friend_detail_concern);
		friendConcernBtn.setOnClickListener(this);
		
		sharedMyListAdapter = new FriendDetailSharedListAdapter(this, myShareData);
		sharedMyList = (XListView) findViewById(R.id.friend_detail_myshare_list);
		sharedMyList.setAdapter(sharedMyListAdapter);
		sharedMyList.setPullLoadEnable(false);
		
		sharedFriendListAdapter = new SharedFriendListAdapter(this, friendSharedData);
		shareFriendList = (XListView) findViewById(R.id.friend_detail_friendshare_list);
		shareFriendList.setAdapter(sharedFriendListAdapter);
		shareFriendList.setPullLoadEnable(false);
		
		mySharedRadioButton = (RadioButton) findViewById(R.id.friend_tabbar_myshared);
		mySharedRadioButton.setOnClickListener(this);
		
		friendSharedRadioButton = (RadioButton) findViewById(R.id.friend_tabbar_friendshared);
		friendSharedRadioButton.setOnClickListener(this);
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		
		case R.id.friend_tabbar_myshared: {
			VisibilityUtil.setVisibilityUI(sharedMyList, true);
			VisibilityUtil.setVisibilityUI(shareFriendList, false);
			break;
		}
		
		case R.id.friend_tabbar_friendshared : {
			VisibilityUtil.setVisibilityUI(sharedMyList, false);
			VisibilityUtil.setVisibilityUI(shareFriendList, true);
			break;
		}
		
		case R.id.friend_detail_concern : {
			//弹出好友取消关注确认框
			AlertDialogHelp.showConfirmDialog(this, "取消关注", 
					"确定取消对该好友的关注?"
					, new OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							//确认要取消好友的关注, 就调用取消关注的请求
							deleteFriendRequest();
						}
					}, null);
		}
		
		}

	}

	@Override
	protected void logic() {
		//初始化json处理器
		if (friendDetailJsonHandle == null) {
			friendDetailJsonHandle = new FriendDetailJsonHandle(this);
		}
		
		// 取出传递过来的好友信息并显示出来
		friendInfoBean = (FriendInfoBean) getIntent().
				getSerializableExtra(Contract.BUNDLE_FRIEND_INFO);
		friendNameText.setText(friendInfoBean.getFriendName());
	}
	
	/**
	 * 取消好友关注的请求
	 */
	private void deleteFriendRequest() {
		FriendBean friendBean = new FriendBean();
		friendBean.setAct(AndroidRestContract.DELETE_FRIEND);
		friendBean.setFriendId(friendInfoBean.getFriendId());
		friendDetailJsonHandle.postTo(AndroidRestContract.REST_FRIEND, friendBean);
	}
	
	/**
	 * 取消好友关注的响应
	 */
	public void deleteFriendResponse() {
		isDeleted = true;
		friendConcernBtn.setText("关注");
		backHomeOperate();
	}
	
	public void refreshMyShare() {
		GetSharedBean getSharedBean = new GetSharedBean();
		getSharedBean.setAct(AndroidRestContract.FRIEND_DETAIL_MYSHARED);
		
		friendDetailJsonHandle.postTo(
				AndroidRestContract.REST_FRIEND, getSharedBean);
	}
	
	public void setMySharedData(List<GetSharedBean> myShareData) {
		this.myShareData = myShareData;
		sharedMyListAdapter.setData(myShareData);
	}
	
	public void refreshFriendShare() {
		GetSharedBean getSharedBean = new GetSharedBean();
		getSharedBean.setAct(AndroidRestContract.FRIEND_DETAIL_FRIENDSHARED);
		
		friendDetailJsonHandle.postTo(
				AndroidRestContract.REST_FRIEND, getSharedBean);
	}
	
	public void setFriendSharedData(List<GetSharedBean> friendShareData) {
		this.friendSharedData = friendShareData;
		sharedFriendListAdapter.setData(friendShareData);
	}

	public void deleteMyShare(int i) {
		ShareBean b = new ShareBean();
		b.setAct(AndroidRestContract.FRIEND_DETAIL_DELETE_MYSHARED);
		b.setPosition(i);
		b.setFriendName(friendInfoBean.getFriendName());
		System.out.println("删除前的" + i);
		b.setPath(myShareData.get(i).getHomeBoxPath());
		friendDetailJsonHandle.postTo(
				AndroidRestContract.REST_FRIEND, b);
	}

	public void downloadMyShare(int i) {
		System.out.println("下载：" + i);
		ShareBean b = new ShareBean();
		b.setFriendName(friendInfoBean.getFriendName());
		b.setAct(AndroidRestContract.FRIEND_DETAIL_DOWNLOAD_MYSHARED);
		b.setPath(myShareData.get(i).getHomeBoxPath());
		friendDetailJsonHandle.postTo(
				AndroidRestContract.REST_FRIEND, b);
		this.toastHandler("正在下载！");
	}


	public void deleteFriendShare(int i) {
		ShareBean b = new ShareBean();
		b.setAct(AndroidRestContract.FRIEND_DETAIL_DELETE_FRIENDSHARED);
		b.setPosition(i);
		System.out.println("删除前的" + i);
		b.setPath(friendSharedData.get(i).getHomeBoxPath());
		friendDetailJsonHandle.postTo(AndroidRestContract.REST_FRIEND, b);

	}

	public void downloadFriendShare(int i) {
		System.out.println("下载：" + i);
		ShareBean b = new ShareBean();
		b.setAct(AndroidRestContract.FRIEND_DETAIL_DOWNLOAD_FRIENDSHARED);
		b.setPath(friendSharedData.get(i).getHomeBoxPath());
		friendDetailJsonHandle.postTo(AndroidRestContract.REST_FRIEND, b);
		this.toastHandler("正在下载！");
	}
	
	@Override
	public void backHomeOperate() {
		setResult();
		finish();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		setResult();
		return super.onKeyDown(keyCode, event);
	}
	
	/**
	 * 设置返回好友列表的结果
	 */
	public void setResult() {
		Intent data = new Intent();
		data.putExtra(Contract.BUNDLE_FRIEND_DETAIL_ISDELETED, isDeleted);
		setResult(RESULT_OK, data);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		setActionBarBack(Contract.TITLE_FRIEND_DETAIL_NAME);
		return false;
	}

}
